package com.cr.admin.business.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cr.admin.business.order.dao.mapper.OrderMapper;
import com.cr.admin.business.order.dao.repository.OrderRepository;
import com.cr.admin.business.order.pojo.entity.Order;
import com.cr.admin.business.order.pojo.param.OrderSaveParam;
import com.cr.admin.business.order.pojo.param.OrderUpdateParam;
import com.cr.admin.business.order.pojo.query.OrderQuery;
import com.cr.admin.business.order.service.OrderService;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private OrderRepository orderRepository;

    @Override
    public List<Order> list(OrderQuery orderQuery) {
        return orderRepository.selectList(orderQuery);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        orderRepository.deleteByIds(ids);
    }

    @Override
    public void updateById(OrderUpdateParam orderUpdateParam) {
        orderRepository.updateById(orderUpdateParam);
    }

    @Override
    public Order selectById(Long id) {
        return orderRepository.selectById(id);
    }

    @Override
    public List<Order> selectList(Object o) {
        return Collections.emptyList();
    }

    @Override
    public void saveOrder(OrderSaveParam orderSaveParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveParam, order);

        // 生成随机订单号，假设你希望订单号保持唯一性
        String orderNumber = generateUniqueOrderNumber().toString();
        order.setId(orderNumber);

        // 设置订单时间为当前时间
        order.setCreateTime(new Date());

        //设置默认状态为1
        order.setOrderStatus("1");

        //根据传过来的日期与时间戳租车,起始时间至租车结束时间计算出租车时长
        long rentalDuration = calculateRentalDuration(orderSaveParam.getStartDate(), orderSaveParam.getEndDate());
        order.setRentalDuration(rentalDuration);

        //设置支付状态默认为0
        order.setPaymentStatus("0");

        // 插入订单到数据库
        orderMapper.insert(order);
    }

    // 生成唯一的订单号
    private Long generateUniqueOrderNumber() {
        // 这里可以根据业务需求生成唯一的订单号，例如使用数据库的序列、UUID、时间戳+随机数等方式生成
        Random random = new Random();
        long currentTime = System.currentTimeMillis();
        long orderNumber = currentTime * 1000 + random.nextInt(1000); // 保证足够唯一性
        return orderNumber;
    }

    // 计算租车时长的方法，假设 startTime 和 endTime 是 Date 类型
    private long calculateRentalDuration(Date startTime, Date endTime) {
        long startMillis = startTime.getTime();
        long endMillis = endTime.getTime();
        return (endMillis - startMillis) / (1000 * 60 * 60); // 返回租车时长，单位为小时
    }



}
